package com.zzl.finance.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzl.finance.data.dao.AddressBookDao;
import com.zzl.finance.data.dto.AddressBookAddDto;
import com.zzl.finance.data.dto.AddressBookUpdateDto;
import com.zzl.finance.data.dto.AddressBookVo;
import com.zzl.finance.data.entity.AddressBookEntity;
import com.zzl.finance.data.service.AddressBookService;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.serurity.entity.User;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.QmBeanUtils;
import com.zzl.general.common.utils.Query;
import com.zzl.general.common.utils.UuidUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.*;


/**
 * @author sunpp
 * @date 2021-11-29 13:17:46
 */
@Service("addressBookService")
public class AddressBookServiceImpl extends ServiceImpl<AddressBookDao, AddressBookEntity> implements AddressBookService {

    /**
     * 分页查询
     * 联系人名称 userName
     * 所属公司 userEnterprise
     * 用户企业id enterpriseId
     *
     * @param params 查询参数
     * @return 分页数据
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        /*TODO*/
        return null;
    }

    /**
     * 新增
     *
     * @param addressOokAddDto 新增实体
     * @param user             当前登录用户
     * @throws SysException 业务异常
     */
    @Override
    public void saveAddressBook(AddressBookAddDto addressOokAddDto, User user) throws SysException {
        // 手机号的 唯一性校验
        LambdaQueryWrapper<AddressBookEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBookEntity::getUserPhone, addressOokAddDto.getUserPhone())
                .eq(AddressBookEntity::getEnterpriseId, user.getEnterpriseId());
        List<AddressBookEntity> list = this.list(queryWrapper);
        if (!CollectionUtils.isEmpty(list)) {
            throw new SysException(addressOokAddDto.getUserPhone() + "已经存在");
        }

        AddressBookEntity addressOokEntity = new AddressBookEntity();
        BeanUtils.copyProperties(addressOokAddDto, addressOokEntity);

        addressOokEntity.setCreatorId(user.getId());
        if (!ObjectUtils.isEmpty(user.getOrgId())) {
            addressOokEntity.setOrgId(user.getOrgId());
        }
        addressOokEntity.setEnterpriseId(user.getEnterpriseId());
        addressOokEntity.setDeletedStatus(0);
        addressOokEntity.setUpdateTime(new Date());
        addressOokEntity.setId(UuidUtils.UUID());
        if (!this.save(addressOokEntity)) {
            throw new SysException("A00001", "保存失败！");
        }
    }

    private void verifyPhone(String phone) throws SysException {
        LambdaQueryWrapper<AddressBookEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBookEntity::getUserPhone, phone);
        List<AddressBookEntity> list = this.list(queryWrapper);
        if (!CollectionUtils.isEmpty(list)) {
            throw new SysException(phone + "已经存在");
        }
    }

    /**
     * 查询通讯录 下拉框公司数据
     *
     * @return 返回公司数据
     */
    @Override
    public List<Map<String, Object>> pullDownValue(User user) {
        List<String> addressBookEntities = this.baseMapper.pullDownValue(user.getEnterpriseId());
        if (CollectionUtils.isEmpty(addressBookEntities)) {
            return new ArrayList<>();
        }
        List<Map<String, Object>> list = new ArrayList<>();
        for (String bookEntity : addressBookEntities) {
            Map<String, Object> map = new HashMap<>(1);
            map.put("value", bookEntity);
            list.add(map);
        }
        return list;
    }

    /**
     * 设置/取消 联系人是否为 常用联系人
     *
     * @param id   id
     * @param user 登录用户
     * @return 提示消息
     * @throws SysException 业务异常
     */
    @Override
    public String isTop(String id, User user) throws SysException {
        if (ObjectUtils.isEmpty(id)) {
            throw new SysException("请传入当前取消置顶的 id");
        }
        AddressBookEntity entity = this.getById(id);
        Integer isTop = 0;
        if (isTop.equals(entity.getIsTop())) {
            isTop = 1;
        }
        entity.setIsTop(isTop);
        entity.setUpdaterId(user.getId());
        entity.setUpdateTime(new Date());
        this.updateById(entity);
        return entity.getIsTop().equals(0) ? "取消成功" : "设置常用联系人成功";
    }

    /**
     * 修改
     *
     * @param addressBookUpdateDto 修改实体
     * @param user                 当前登录用户
     * @throws SysException 业务异常
     */
    @Override
    public void updateAddressOokById(AddressBookUpdateDto addressBookUpdateDto, User user) throws SysException {

        // 手机号唯一性校验
        AddressBookEntity entity = this.getById(addressBookUpdateDto.getId());
        if (!ObjectUtils.isEmpty(entity)) {
            if (!entity.getUserPhone().equals(addressBookUpdateDto.getUserPhone())) {
                // 对新加的手机号 进行唯一性的校验
                verifyPhone(addressBookUpdateDto.getUserPhone());
            }
        }

        BeanUtils.copyProperties(addressBookUpdateDto, entity);
        entity.setUpdateTime(new Date());
        entity.setUpdaterId(user.getId());
        if (!this.updateById(entity)) {
            throw new SysException("A00002", "修改失败！");
        }
    }

}